package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.dto.UserAddressDto;
import com.group7.mall.entity.TbGoods;
import com.group7.mall.entity.TbUser;
import com.group7.mall.entity.TbUserAddress;
import com.group7.mall.service.UserAddressService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.PublicKey;
import java.util.List;

@RestController
@RequestMapping("user-address")
public class CustomerAddressController {

    @Resource
    private UserAddressService userAddressService;

    @PostMapping("show-all-address")
    public Result<List<UserAddressDto>> showAllUserAddress(@RequestBody TbUser tbUser) {
        return Result.ok(userAddressService.queryAllUserAddress(tbUser.getUserOpenId()));
    }

    @PostMapping("change-default-address")
    public Result changeDefaultAddress(@RequestBody UserAddressDto userAddressDto) {
        Result result = Result.ok();
        Integer i = userAddressService.changeDefaultAddress(userAddressDto);
        if (i == 0) {
            result.setMessage("修改默认地址失败！数据库无相关信息");
            result.setData(0);
            return result;
        } else {
            result.setMessage("修改默认地址成功！");
            result.setData(1);
            return result;
        }
    }

    @PostMapping("add-address")
    public Result<UserAddressDto> addAddress(@RequestBody UserAddressDto userAddressDto) {
        Result result = Result.ok();
        return Result.ok(userAddressService.insert(userAddressDto));
    }

    @PostMapping("update-address")
    public Result updateAddress(@RequestBody UserAddressDto userAddressDto) {
        Result result = Result.ok();
        userAddressDto = userAddressService.update(userAddressDto);
        if (userAddressDto == null) {
            result.setMessage("修改失败！");
            result.setData(0);
            return result;
        } else {
            result.setMessage("修改成功！");
            result.setData(1);
            return result;
        }
    }

    @PostMapping("delete-address")
    public Result deleteAddress(@RequestBody UserAddressDto userAddressDto) {
        int i = userAddressService.deleteById(userAddressDto.getAddressId());
        Result result = Result.ok();
        if (i == 1) {
            result.setMessage("删除成功！");
            result.setData(1);
            return result;
        } else {
            result.setMessage("删除失败！");
            result.setData(0);
            return result;
        }
    }

    @PostMapping("show-one-address")
    public Result showOneAddress(@RequestBody UserAddressDto userAddressDto) {
        return Result.ok(userAddressService.showOneAddress(userAddressDto));
    }

    @PostMapping("query-default-address-id")
    public Result<Integer> queryDefaultAddressId(@RequestBody TbUser tbUser) {
        Result result = Result.ok();
        result.setMessage("查询成功！data值为默认地址id");
        result.setData(userAddressService.queryDefaultAddressId(tbUser));
        return result;
    }
}
